﻿//////////////////////////////////////////////////////////////////
//
// LoggerPipe.cs
//
// Copyright (c) 2012 Dan Pike.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Drawing;

namespace Utilities
{
   public class LoggerPipe
   {
      public delegate void HasMessagesEventHandler(object sender, EventArgs e);
      public static event HasMessagesEventHandler hasMessagesEvent;

      public class LoggingLevel
      {
         public static readonly int Audit = 5000;
      };

      ////////////////////////////////////////////////////////////////////////
      //
      public class LoggingEvent
      {
         public LoggingEvent(log4net.Core.LoggingEvent loggingEvent, string renderedText)
         {
            loggingEvent_ = loggingEvent;
            renderedText_ = renderedText;
         }

         public string renderedText { get { return renderedText_; } }
         public int loggingLevel { get { return loggingEvent_.Level.Value; } }

         public Color foreColor
         {
            get
            {
               int checkValue = loggingEvent_.Level.Value;
               if (log4net.Ext.Audit.AuditLogImpl.levelAudit.Value == checkValue)
               {
                  return Color.Blue;
               }
               if (log4net.Core.Level.Debug.Value == checkValue)
               {
                  return Color.Gray;
               }
               if (log4net.Core.Level.Warn.Value == checkValue)
               {
                  return Color.Yellow;
               }
               if (log4net.Core.Level.Error.Value == checkValue)
               {
                  return Color.White;
               }
               if (log4net.Core.Level.Fatal.Value == checkValue)
               {
                  return Color.White;
               }
               return Color.Black;
            }
         }

         public Color backColor
         {
            get
            {
               int checkValue = loggingEvent_.Level.Value;
               if (log4net.Core.Level.Warn.Value == checkValue)
               {
                  return Color.DarkSlateBlue;
               }
               if (log4net.Core.Level.Error.Value == checkValue)
               {
                  return Color.OrangeRed;
               }
               if (log4net.Core.Level.Fatal.Value == checkValue)
               {
                  return Color.Red;
               }
               return Color.White;
            }
         }

         private log4net.Core.LoggingEvent loggingEvent_ = null;
         private string renderedText_ = "";
      };

      ////////////////////////////////////////////////////////////////////////
      //
      static LoggerPipe()
      {
         pipe_ = new Queue<LoggingEvent>();
      }

      ////////////////////////////////////////////////////////////////////////
      //
      public static void write(LoggingEvent loggingEvent)
      {
         try
         {
            lock(pipe_)
            {
               pipe_.Enqueue(loggingEvent);
            }
            if (null != hasMessagesEvent)
            {
               hasMessagesEvent(null, null);
            }
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
      }

      ////////////////////////////////////////////////////////////////////////
      //
      public static bool read(ref LoggingEvent loggingEvent)
      {
         try
         {
            lock (pipe_)
            {
               if (0 < pipe_.Count)
               {
                  loggingEvent = pipe_.Dequeue();
                  return true;
               }
            }
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return false;
      }

      ////////////////////////////////////////////////////////////////////////
      //
      public static int count
      {
         get
         {
            try
            {
               lock (pipe_)
               {
                  return pipe_.Count;
               }
            }
            catch (System.Exception ex)
            {
               logger_.exception(ex);
            }
            return 0;
         }
      }

      ////////////////////////////////////////////////////////////////////////
      //
      private static Logger logger_ = new Logger();
      private static Queue<LoggingEvent> pipe_ = new Queue<LoggingEvent>();
   }
}
